package me.linczh.framework.persiste;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBStoreHolder {
	public static String URL = "jdbc:mysql://localhost:3306/bank";
	public static String USERNAME = "root";
	public static String PASSWORD = "123456";

	private DBStoreHolder() {}
	
	public static void init() {
		try {
			Class.forName("com.mysql.jdbc.Driver");
			Connection conn = DriverManager.getConnection(DBStoreHolder.URL, DBStoreHolder.USERNAME, DBStoreHolder.PASSWORD);
			Statement s = conn.createStatement();
			ResultSet result = s.executeQuery("select max(right(account, 4)) from account_tbl");
			if (result.next()) {
				try {
					IdGenerator.initAccountId(Integer.parseInt(result.getString(1)) + 1);
				} catch (NumberFormatException e) {
					IdGenerator.initAccountId(1000);
				}
			}

			result = s.executeQuery("select max(right(employee, 4)) from employee_tbl");
			if (result.next()) {
				IdGenerator.initEmployeeId(Integer.parseInt(result.getString(1)) + 1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
